const getModifyTemplate = (componenetName) => [
  //template
  {
    repeatReg: new RegExp(`<${componenetName}`),
    targetReg: /(<\/\S*>[\s]*<\/template>[\s]*?<script>)/,
    tagetReplace: `<${componenetName} fileType="" :visible.sync="dialogShow.${componenetName}" @confirm="saveDialog('${componenetName}', $event)" />\n$1`,
  },
  // import
  {
    repeatReg: new RegExp(
      `import\\s${componenetName}\\sfrom\\s['"']\\.\\/${componenetName}\\.vue['"']`
    ),
    targetReg: /<script>([\s\S]*?)export/,
    tagetReplace: `<script>$1import ${componenetName} from '@/components/biz/basic/Dialog/uploadDialog.vue';\nimport { uploadFile } from '@/model/api/common/commonApi';

export`,
  },
  // components
  {
    repeatReg: new RegExp(`components:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /components:\s*{/,
    addOptionReg: /(name:.*),/,
    addOptionReplace: `$1,
    components: {
      ${componenetName},
    },`,
    targetReg: /components:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(componenetName);
      return `components:{${keys.join(",")}},`;
    },
  },
  {
    // dialogShow
    repeatReg: new RegExp(`dialogShow:\\s*{[^}]*\\b${componenetName}\\b`),
    checkOptionReg: /dialogShow:\s*{/,
    addOptionReg: /data\(\)\s*{([\s]*?)return\s*{/,
    addOptionReplace: `data\(\) {$1return {
    dialogShow: {
      ${componenetName}: false,
    },`,
    targetReg: /dialogShow:\s*{([^}]*)},/,
    tagetReplace: (match, p1) => {
      // 去掉p1中空白字符
      p1 = p1.replace(/\s+/g, "");
      //  如果末尾有，则去掉
      if (p1.endsWith(",")) {
        p1 = p1.slice(0, -1);
      }
      const keys = p1
        .split(",")
        .filter((t) => t !== "\n")
        .filter((t) => !!t);
      keys.push(`${componenetName}:false`);
      return `dialogShow:{${keys.join(",")}},`;
    },
  },
  {
    // saveDialog
    repeatReg: null,
    checkOptionReg: /saveDialog\(\s*type\s*,\s*data\s*\)/,
    addOptionReg: /methods:\s*{/,
    addOptionReplace: `methods: {
      saveDialog(type, data) {
        switch (type) {
          case '${componenetName}': {
            let fileParam = new File([data.raw], data.name, {
              type: data.raw.type,
              size: data.size,
            });
            let formData = new FormData();
            formData.append('file', fileParam);
            uploadFile(formData).then(res => {
              if (res.success) {
                this.$message.success('上传成功');
                this.dialogShow.uploadDialog = false;
                // this.fileTableData.push({
                //   annexOriginalFilename: res.data.originalFilename,
                //   annexSize: res.data.fileSize,
                //   annexPath: res.data.filePath,
                //   annexName: res.data.fileName,
                //   remarks: '',
                //   fileType: '',
                // });
              }
            });
          }
          break;
        }
      },
      showDialog(type) {
        this.dialogShow[type] = true;
      },
     `,
    targetReg: /saveDialog\(type, data\) \{\s*switch \(type\) \{/,
    tagetReplace: `saveDialog(type, data) {
      switch (type) {
        case '${componenetName}': {
          let fileParam = new File([data.raw], data.name, {
            type: data.raw.type,
            size: data.size,
          });
          let formData = new FormData();
          formData.append('file', fileParam);
          uploadFile(formData).then(res => {
            if (res.success) {
              this.$message.success('上传成功');
              this.dialogShow.uploadDialog = false;
              // this.fileTableData.push({
              //   annexOriginalFilename: res.data.originalFilename,
              //   annexSize: res.data.fileSize,
              //   annexPath: res.data.filePath,
              //   annexName: res.data.fileName,
              //   remarks: '',
              //   fileType: '',
              // });
            }
          });
        }
        break;`,
  },
];
module.exports = {
  getModifyTemplate,
};
